import requests
from bs4 import BeautifulSoup
import os
import time


def fetch_formula_elements(url, class_name="formula"):
    """
    获取网页中指定 class 的标签及其子标签的 HTML 信息

    Args:
        url: 网页URL
        class_name: 要查找的class名称

    Returns:
        list: 包含标签HTML内容的列表
    """
    try:
        # 设置请求头，模拟浏览器访问
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }

        print(f"正在访问网页: {url}")
        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()  # 如果请求失败则抛出异常

        # 检查编码并设置
        if response.encoding == 'ISO-8859-1':
            response.encoding = response.apparent_encoding

        print("网页访问成功，开始解析...")

        # 使用BeautifulSoup解析HTML
        soup = BeautifulSoup(response.text, 'html.parser')

        # 查找所有class为"formula"的标签
        formula_elements = soup.find_all(class_=class_name)

        print(f"找到 {len(formula_elements)} 个 class='{class_name}' 的标签")

        # 提取每个标签及其所有子标签的完整HTML
        formula_html_list = []
        for i, element in enumerate(formula_elements, 1):
            formula_html = {
                'index': i,
                'html': str(element),
                'text': element.get_text(strip=True)
            }
            formula_html_list.append(formula_html)

        return formula_html_list

    except requests.exceptions.RequestException as e:
        print(f"网络请求错误: {e}")
        return []
    except Exception as e:
        print(f"解析过程中发生错误: {e}")
        return []


def save_to_file(formula_html_list, filename="formula_elements.html"):
    """
    将提取的HTML内容保存到文件

    Args:
        formula_html_list: 包含HTML内容的列表
        filename: 保存的文件名
    """
    try:
        with open(filename, 'w', encoding='utf-8') as file:
            # 写入文件头部
            file.write('<!DOCTYPE html>\n<html>\n<head>\n<meta charset="UTF-8">\n')
            file.write('<title>提取的Formula元素</title>\n</head>\n<body>\n')
            file.write(f'<h1>从网页提取的 class="formula" 元素</h1>\n')
            file.write(f'<p>共找到 {len(formula_html_list)} 个元素</p>\n<hr>\n')

            # 写入每个formula元素
            for item in formula_html_list:
                file.write(f'<div class="formula-item">\n')
                file.write(f'<h2>元素 #{item["index"]}</h2>\n')
                file.write(f'<div class="original-html">\n')
                file.write(item['html'] + '\n')
                file.write('</div>\n')
                file.write(f'<div class="text-content">\n')
                file.write(f'<p><strong>文本内容:</strong> {item["text"]}</p>\n')
                file.write('</div>\n')
                file.write('<hr>\n</div>\n\n')

            # 写入文件尾部
            file.write('</body>\n</html>')

        print(f"内容已保存到文件: {filename}")
        return True

    except Exception as e:
        print(f"保存文件时发生错误: {e}")
        return False


def save_as_raw_html(formula_html_list, filename="formula_elements_raw.html"):
    """
    保存原始HTML内容（不添加额外格式）
    """
    try:
        with open(filename, 'w', encoding='utf-8') as file:
            file.write('<!-- 提取的 class="formula" 元素原始HTML -->\n')
            for item in formula_html_list:
                file.write(f'<!-- 元素 #{item["index"]} -->\n')
                file.write(item['html'] + '\n')
                file.write('<!-- --------------------------------- -->\n\n')

        print(f"原始HTML已保存到文件: {filename}")
        return True
    except Exception as e:
        print(f"保存原始HTML时发生错误: {e}")
        return False


def main():
    """主函数"""
    # 目标URL
    url = "https://wiki.biligame.com/dsp/%E5%BC%95%E5%8A%9B%E7%9F%A9%E9%98%B5"

    # 获取formula元素
    formula_elements = fetch_formula_elements(url, "formula")

    if not formula_elements:
        print("未找到任何formula元素，程序结束")
        return

    # 显示找到的内容摘要
    print("\n找到的内容摘要:")
    for item in formula_elements:
        print(f"元素 #{item['index']}:")
        print(f"  文本预览: {item['text'][:100]}...")
        print(f"  HTML长度: {len(item['html'])} 字符")
        print()

    # 保存到格式化的HTML文件
    save_to_file(formula_elements, "output/formula_elements_formatted.html")

    # 保存原始HTML
    save_as_raw_html(formula_elements, "output/formula_elements_raw.html")

    # 同时保存为文本文件，方便查看
    try:
        with open("output/formula_elements_summary.txt", 'w', encoding='utf-8') as file:
            file.write(f"从 {url} 提取的 class='formula' 元素\n")
            file.write(f"提取时间: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
            file.write(f"共找到 {len(formula_elements)} 个元素\n\n")

            for item in formula_elements:
                file.write(f"=== 元素 #{item['index']} ===\n")
                file.write(f"文本内容: {item['text']}\n")
                file.write(f"HTML内容:\n{item['html']}\n")
                file.write("=" * 50 + "\n\n")

        print("摘要已保存到: formula_elements_summary.txt")
    except Exception as e:
        print(f"保存摘要文件时发生错误: {e}")


if __name__ == "__main__":
    # 检查是否需要安装依赖
    # try:
    #     import requests
    #     from bs4 import BeautifulSoup
    # except ImportError:
    #     print("请先安装所需依赖:")
    #     print("pip install requests beautifulsoup4")
    #     exit(1)

    main()